<template>
  <j-modal
    title="发布"
    width="50%"
    okText="发布"
    :visible.sync="visible"
    :ok-close="false"
    @ok="handleOK">
    <a-spin :spinning="loading">
      <border-box title="发布设置">
        <a-form-model v-bind="layout" :model='form' :rules="rules" ref="formModel">
          <a-form-model-item label="发布设置">
            <a-radio-group v-model="form.releaseMode">
              <a-radio value="1">立即发布</a-radio>
              <a-radio value="2">定时发布</a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item v-if="form.releaseMode=='2'" label="发布时间" prop="releaseSetTime">
            <j-date v-model="form.releaseSetTime" :showTime="true"
                    date-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="请选择发布时间"></j-date>
          </a-form-model-item>
          <a-form-model-item label="发布者" prop="authorName">
            <a-input v-model="form.authorName" placeholder="请填写发布者姓名"></a-input>
          </a-form-model-item>
        </a-form-model>
      </border-box>
    </a-spin>
  </j-modal>
</template>

<script>
  //发布时间
  import BorderBox from "./BorderBox";
  import JDate from '@/components/jeecg/JDate'
  import {postAction} from "@/api/manage";

  const publishUrl = {
    content: 'content/contentInfo/publishContent',
    activity: 'activity/activityInfo/publishActivity'
  };

  export default {
    name: "PublishSetting",
    components: {BorderBox, JDate},
    props: ['params'],
    data() {
      return {
        visible: false,
        layout: {
          labelCol: {md: 8, lg: 7},
          wrapperCol: {md: 16, lg: 17},
        },
        form: {
          releaseMode: '1'
        },
        loading: false,
        taskNo: null,
        contentType: '',
        rules: {
          releaseSetTime: [{required: true, message: '请选择发布时间', trigger: 'blur'}],
          authorName: [{required: true, message: '请填写发布者姓名', trigger: 'blur|change'}]
        }
      }
    },
    methods: {
      selfValidate() {
        return new Promise((resolve, reject) => {
          this.$refs.formModel.validate(validate => {
            if (validate) {
              resolve({key: 'publishInfo', value: this.form});
            } else {
              reject('请填写发布必要信息');
              return false;
            }
          })
        })
      },
      getFormInfo() {
        return this.form;
      },
      handleOK() {
        this.$refs.formModel.validate(validate => {
          if (validate) {
            this.loading = true;
            postAction(publishUrl[this.contentType], {taskNo: this.taskNo, publishInfo: this.form}).then(res => {
              if (res.success) {
                this.$message.success('发布成功');
                this.close();
                this.$emit('refresh');
              } else {
                this.$message.error(res.message);
              }
            }).finally(() => {
              this.loading = false;
            })
          } else {
            return false;
          }
        })
      },
      show(taskNo, contentType) {
        this.taskNo = taskNo;
        this.contentType = contentType;
        this.form = {
          releaseMode: '1'
        };
        this.visible = true;
      },
      close() {
        this.visible = false;
      }
    }
  }
</script>

<style scoped>

</style>